const resContent = require('../util/ResValue')
const Where = require('../util/SqlWhere');
const dbUtil = require('../util/dbutil');

class Validation {
    /**
     * 验证字段是否缺失
     *@param {object} body - 请求体对象
     *@param {String[]} name - 需要验证的字段数组
     *@return {Promise} string - string 不通过原因
     *@return {Promise} false - false 通过
     **/
    static async theFieldIsMissingExists(body, toBeVerified) {
        let resText = '';
        for (let name of toBeVerified) {
            if (!body[name] && body[name] !== 0) {
                resText += `-${name}参数为空`;
            }
        }
        if (resText !== '') {
            return resText;
        }
        return false;
    }

    /**
     *查询城市是否存在
     *@param {string} name - 前端传过来的城市id字段
     *@param {object} body - 请求体对象
     *@returns {Promise} - 存在返回true，不存在返回false
     **/
    static async sqlIsCity(name, body) {
        const isCity = await new Where('select * from city', body)
            .addQueryCriteria('id = ?', body[name])
            .setQueryMode(dbUtil.getRow)
            .executeTheQuery();
        return isCity !== undefined;
    }

    //验证此天气是否存在
    static async isWeather(ctx, next) {
        const body = ctx.request.body;
        const isWeather = await new Where('select * from weather', body)
            .addQueryCriteria('id = ?', body['id'])
            .setQueryMode(dbUtil.getRow)
            .executeTheQuery();
        //天气不存在
        if (!isWeather) {
            ctx = resContent.weatherNotExist(ctx);
            return 0;
        }
        await next();
    }

    //验证城市是否存在
    static isCity(bodyId) {
        //闭包保存传入的bodyId
        const name = bodyId;
        return async (ctx, next) => {
            const body = ctx.request.body;
            //城市不存在
            if (!await this.sqlIsCity(name, body)) {
                ctx = resContent.citiesDoNotExist(ctx);
                return 0;
            }
            await next();
        }
    }

    //验证父级城市是否存在
    static async isCitiesArePresence(ctx, next) {
        let body = JSON.parse(JSON.stringify(ctx.request.body));
        //如果是一级城市则不进行验证此字段
        if (body.level === 1) {
            await next();
            return 0;
        }
        //修改查询条件
        body.level -= 1;
        body.id = body.fatherId;
        //验证父级城市是否存在
        const isCity = await new Where('select * from city')
            .addQueryCriteriaMultiple(body, ['level', 'id'])
            .setQueryMode(dbUtil.getRow)
            .executeTheQuery();
        //层级为body.level-1的父级城市不存在
        if (isCity === undefined) {
            ctx = resContent.parentCityNotExist(ctx);
            return 0;
        }
        await next();
    }

    //验证城市等级是否存在-1省-2市-3区
    static async isCityLevel(ctx, next) {
        const body = ctx.request.body;
        //没有此城市等级
        if (body.level !== 1 && body.level !== 2 && body.level !== 3) {
            ctx = await resContent.thereIsNoSuchCityLevel(ctx);
            return 0;
        }
        await next();
    }
}


module.exports = Validation;